var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    page: 2,
  },
  indentlist:function(e){
    console.log(e)
    var that = this;
    this.setData({
      id: e.target.id
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.Id
    })
    this.initSystemInfo();
    this.setData({
      currentTab: parseInt(options.id),
      isStatus: options.otype
    });

    if (this.data.currentTab == 4) {
      this.loadReturnOrderList();
    } else {
      this.loadOrderList(options.type);
    }
    wx.setStorage({
      key: "ostatus",
      data: options.type
    })
  },
  loadOrderList: function (sta) {
    var that = this;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Order/index',
      method: 'post',
      data: {
        uid: app.d.userId,
        // order_type: that.data.isStatus,
        order_type: sta
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //--init data        
        var status = res.data.status;
        var list = res.data.ord;
        console.log(list)
        that.setData({
          orderlist: list,
          winHeight: 900
        });
        // switch (that.data.currentTab) {
        //   case 0:
        //     that.setData({
        //       orderList0: list,
        //       winHeight: 900,
        //     });
        //     break;
        //   case 1:
        //     that.setData({
        //       orderList1: list,
        //       winHeight: 900,
        //     });
        //     break;
        //   case 2:
        //     that.setData({
        //       orderList2: list,
        //       winHeight: 900,
        //     });
        //     break;
        //   case 3:
        //     that.setData({
        //       orderList3: list,
        //       winHeight: 900,
        //     });
        //     break;
        //   case 4:
        //     that.setData({
        //       orderList4: list,
        //       winHeight: 900,
        //     });
        //     break;
        // }
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    });
  },
  chagestu:function(e){
    console.log(e.currentTarget.dataset.type)
    wx.setStorage({
      key: "ostatus",
      data: e.currentTarget.dataset.type
    })
    var that = this;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Order/index',
      method: 'post',
      data: {
        uid: app.d.userId,
        // order_type: that.data.isStatus,
        order_type: e.currentTarget.dataset.type,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //--init data        
        var status = res.data.status;
        var list = res.data.ord;
        console.log(list)
        that.setData({
          orderlist: list,
          winHeight: 900
        });
        // switch (that.data.currentTab) {
        //   case 0:
        //     that.setData({
        //       orderList0: list,
        //       winHeight: 900,
        //     });
        //     break;
        //   case 1:
        //     that.setData({
        //       orderList1: list,
        //       winHeight: 900,
        //     });
        //     break;
        //   case 2:
        //     that.setData({
        //       orderList2: list,
        //       winHeight: 900,
        //     });
        //     break;
        //   case 3:
        //     that.setData({
        //       orderList3: list,
        //       winHeight: 900,
        //     });
        //     break;
        //   case 4:
        //     that.setData({
        //       orderList4: list,
        //       winHeight: 900,
        //     });
        //     break;
        // }
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    });
  },
  initSystemInfo: function () {
    var that = this;

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
  },
  payOrderByWechat: function (e) {
    var that = this;
    var order_id = e.currentTarget.dataset.orderid;
    var order_sn = e.currentTarget.dataset.ordersn;

    if (!order_sn || !order_id) {
      wx.showToast({
        title: "订单异常!",
        duration: 2000,
      });
      return false;
    }
    //设定order ID
    that.setData({
      orderId: order_id
    });
    wx.request({
      url: app.d.ceshiUrl + '/Api/Wxpay/wxpay',
      data: {
        order_id: order_id,
        order_sn: order_sn,
        uid: app.d.userId,
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }, // 设置请求的 header
      success: function (res) {
        if (res.data.status == 1) {
          var order = res.data.arr;
          wx.requestPayment({
            timeStamp: order.timeStamp,
            nonceStr: order.nonceStr,
            package: order.package,
            signType: 'MD5',
            paySign: order.paySign,
            success: function (res) {
              that.getcode();
              that.distribution_details_set();
            },
            fail: function (res) {
              wx.showToast({
                title: res,
                duration: 3000
              })
            }
          })
        } else {
          wx.showToast({
            title: res.data.err,
            duration: 2000
          });
        }
      },
      fail: function (e) {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    })
  },
  distribution_details_set: function(){
      wx.request({
        url: app.d.ceshiUrl + '/Api/Wxpay/fxLogSet',
        data: {
          order_id: this.data.order_id,
          uid: app.d.userId,
        },
        method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
      })
  },
  //取消订单
  removeOrder: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.orderid;
    wx.showModal({
      title: '提示',
      content: '你确定要取消订单吗？',
      success: function (res) {
        res.confirm && wx.request({
          url: app.d.ceshiUrl + '/Api/Order/orders_edit',
          method: 'post',
          data: {
            id: orderId,
            type: 'cancel',
          },
          header: {
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            //--init data
            var status = res.data.status;
            if (status == 1) {
              wx.showToast({
                title: '操作成功！',
                duration: 2000
              });
              wx.getStorage({
                key: 'ostatus',
                success: function (res) {
                  // console.log(res.data)
                  that.loadOrderList(res.data);
                }
              })
              // that.loadOrderList();
            } else {
              wx.showToast({
                title: res.data.err,
                duration: 2000
              });
            }
          },
          fail: function () {
            // fail
            wx.showToast({
              title: '网络异常！',
              duration: 2000
            });
          }
        });

      }
    });
  },
  //获取更多
  getMore: function (e) {
    var that = this;
    var winHeight = that.data.winHeight;
    var page = that.data.page;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Order/get_more',
      method: 'post',
      data: {
        uid: app.d.userId,
        order_type: that.data.isStatus,
        page: that.data.page,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var list = res.data.ord;
        if (list == '') {
          wx.showToast({
            title: '没有更多数据！',
            duration: 2000
          });
          return false;
        }
        var len = (list.length) * 120;
        var status = res.data.status;
        that.setData({
          orderlist: that.data.orderlist.concat(list),
          winHeight: winHeight + len,
        });
        // switch (that.data.currentTab) {
        //   case 0:
        //     that.setData({
        //       orderList0: that.data.orderList0.concat(list),
        //       winHeight: winHeight + len,
        //     });
        //     break;
        //   case 1:
        //     that.setData({
        //       orderList1: that.data.orderList1.concat(list),
        //       winHeight: winHeight + len,
        //     });
        //     break;
        //   case 2:
        //     that.setData({
        //       orderList2: that.data.orderList2.concat(list),
        //       winHeight: winHeight + len,
        //     });
        //     break;
        //   case 3:
        //     that.setData({
        //       orderList3: that.data.orderList3.concat(list),
        //       winHeight: winHeight + len,
        //     });
        //     break;
        //   case 4:
        //     that.setData({
        //       orderList4: that.data.orderList4.concat(list),
        //       winHeight: winHeight + len,
        //     });
        //     break;
        // }
        //that.initProductData(data);
        that.setData({
          page: page + 1,
        });
        //endInitData
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.setNavigationBarTitle({
      title: '订单列表',
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
//  onShareAppMessage: function () {
//
//  },
  //确认收货
  recOrder: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.orderid;
    wx.showModal({
      title: '提示',
      content: '你确定已收到宝贝吗？',
      success: function (res) {
        res.confirm && wx.request({
          url: app.d.ceshiUrl + '/Api/Order/orders_edit',
          method: 'post',
          data: {
            id: orderId,
            type: 'receive',
          },
          header: {
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            //--init data
            var status = res.data.status;
            if (status == 1) {
              wx.showToast({
                title: '操作成功！',
                duration: 2000
              });
              that.loadOrderList('receive');
            } else {
              wx.showToast({
                title: res.data.err,
                duration: 2000
              });
            }
          },
          fail: function () {
            // fail
            wx.showToast({
              title: '网络异常！',
              duration: 2000
            });
          }
        });

      }
    });
  },
})